//
//  LeGameObject.cpp
//  Leste
//
//  Created by 王乐 on 2022/1/24.
//

#include "LeGameObject.h"

#include "LeRenderer.h"
#include "LeBehaviour.h"
#include "LeRigidBody.h"

#include "LeScene.h"

LeGameObject::LeGameObject()
{
    ID = -1;
    strName = "";
    m_bIsActive = true;
}

LeGameObject::~LeGameObject()
{
    if( m_pRenderer != nullptr)
    {
        delete m_pRenderer;
    }
    if( m_pRigidBody != nullptr)
    {
        delete m_pRigidBody;
    }
    if( m_pBehaviour != nullptr)
    {
        delete m_pBehaviour;
    }
}

void LeGameObject::initComponents(LeRenderer *pRenderer, LeRigidBody* pRigidBody, LeBehaviour* pBehaviour, LeParticleSystem* pParticleSystem) {
    m_pRenderer = pRenderer;
    m_pRigidBody = pRigidBody;
    m_pBehaviour = pBehaviour;
    m_pParticleSystem = pParticleSystem;
    if(m_pBehaviour != nullptr)
        m_pBehaviour->initBehaviour(pRigidBody, pRenderer, pParticleSystem, this);
}

void LeGameObject::Awake() { 
    if(m_pBehaviour != nullptr)
        m_pBehaviour->Awake();
}

void LeGameObject::PreRender() {
    if(m_pRenderer != nullptr)
        m_pRenderer->preLoopRender();
    if(m_pBehaviour != nullptr)
        m_pBehaviour->PreRender();
}

void LeGameObject::Start() {
    if(m_pBehaviour != nullptr)
        m_pBehaviour->Start();
}


void LeGameObject::RenderSelection()
{
    if(m_pRenderer != nullptr)
        m_pRenderer->renderSelection();
}

void LeGameObject::Update() {
    if(m_pBehaviour != nullptr)
        m_pBehaviour->Update();
}

void LeGameObject::PostWorldStep()
{
    //update transform by rigidbody.
    if(m_pRigidBody != nullptr)
    {
        b2Vec2 position = m_pRigidBody->m_Body->GetPosition();
        m_Transform.setPosition(position.x, position.y, m_Transform.m_Position.z);
        float angle = m_pRigidBody->m_Body->GetAngle();
        m_Transform.setRotation(m_Transform.m_Rotation.x, m_Transform.m_Rotation.y, angle);
    }
}

void LeGameObject::FixedUpdate() {
    if(m_pBehaviour != nullptr)
        m_pBehaviour->FixedUpdate();
}

void LeGameObject::Render() {
    if(m_pRenderer != nullptr)
        m_pRenderer->render();
    if(m_pBehaviour != nullptr)
        m_pBehaviour->Render();
}

void LeGameObject::PostprocessingRender()
{
    if(m_pRenderer != nullptr)
        m_pRenderer->postprocessingRender();
}

void LeGameObject::PostRender() {
    if(m_pRenderer != nullptr)
        m_pRenderer->postLoopRender();
    if(m_pBehaviour != nullptr)
        m_pBehaviour->PostRender();
}

void LeGameObject::OnContactBegin(LeGameObject *pOther)
{
    if(m_pBehaviour != nullptr)
        m_pBehaviour->OnContactBegin(pOther);
}

void LeGameObject::OnContactEnd(LeGameObject *pOther)
{
    if(m_pBehaviour != nullptr)
        m_pBehaviour->OnContactEnd(pOther);
}

void LeGameObject::DestroyGameObject()
{
    LeScene::getInstance()->addGameObjIDtoDestroy(ID);
}

void LeGameObject::DestroyRigidBodyInWorld()
{
    if(m_pRigidBody != nullptr)
    {
        m_pRigidBody->destroyRigidBody();
    }
}

bool LeGameObject::isActive() const
{
    return m_bIsActive;
}

void LeGameObject::setActive(bool isActive)
{
    m_bIsActive = isActive;
}

void LeGameObject::PostprocessingSetup()
{
    if(m_pRenderer != nullptr)
        m_pRenderer->postprocessingSetup();
}
